/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Vista;

import Validacion.ValidaCliente;
import java.awt.event.ItemEvent;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import Modelo.Cliente;
import Modelo.Producto;
import java.util.Iterator;
import java.util.TreeSet;

/**
 *
 * @author bastian
 */
public class Panel_BuscaProducto1 extends javax.swing.JPanel {

    /**
     * Creates new form Panel_Cliente
     */
    public Panel_BuscaProducto1() {
        initComponents();
    }
    
    private VentanaPrincipal V_Principal;
    private boolean desdeFactura, desdeRecepcion;
    
    public void setFrame(JFrame f){
        this.V_Principal= (VentanaPrincipal) f;
        desdeFactura = false;
        desdeRecepcion = false;
    }
    
    public void esRecepcion(){
        desdeRecepcion=true;
        desdeFactura = false;
    
    }
    
    public void esFactura(){
        desdeFactura = true;
        desdeRecepcion = false;
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jRadioButton_IDProducto = new javax.swing.JRadioButton();
        jRadioButton_Descripcion = new javax.swing.JRadioButton();
        label_Buscar481 = new Vista.Imagenes_Label.buscar.Label_Buscar48();
        jLabel_BuscarCliente = new javax.swing.JLabel();
        jRadioButton_Precio = new javax.swing.JRadioButton();
        jTextField_Buscar = new javax.swing.JTextField();
        jButton_Buscar = new javax.swing.JButton();
        jButton_Agregar = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel_BusquedaProductos = new javax.swing.JLabel();
        jLabel_ProductosSeleccionados = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tabla_Producto_BusquedaProductos = new Vista.Tablas.Tabla_Producto();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabla_Producto_ProductosSeleccionados = new Vista.Tablas.Tabla_Producto();
        jButton_Cancelar = new javax.swing.JButton();
        jButton_Aceptar = new javax.swing.JButton();

        setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N
        setMaximumSize(new java.awt.Dimension(1000, 550));
        setMinimumSize(new java.awt.Dimension(1000, 550));
        setName("Cliente");
        setPreferredSize(new java.awt.Dimension(1000, 550));

        jRadioButton_IDProducto.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        jRadioButton_IDProducto.setText("Buscar por ID Producto");
        jRadioButton_IDProducto.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jRadioButton_IDProductoItemStateChanged(evt);
            }
        });

        jRadioButton_Descripcion.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        jRadioButton_Descripcion.setText("Buscar por Descripcion");
        jRadioButton_Descripcion.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jRadioButton_DescripcionItemStateChanged(evt);
            }
        });

        jLabel_BuscarCliente.setFont(new java.awt.Font("Arial", 0, 36)); // NOI18N
        jLabel_BuscarCliente.setText("Buscar Producto");

        jRadioButton_Precio.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        jRadioButton_Precio.setText("Buscar por Precio Unitario");
        jRadioButton_Precio.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jRadioButton_PrecioItemStateChanged(evt);
            }
        });

        jTextField_Buscar.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        jTextField_Buscar.setText("Escriba lo que quiere encontrar...");
        jTextField_Buscar.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTextField_BuscarFocusGained(evt);
            }
        });
        jTextField_Buscar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextField_BuscarKeyTyped(evt);
            }
        });

        jButton_Buscar.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        jButton_Buscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Vista/Imagenes_Files/buscar/16.png"))); // NOI18N
        jButton_Buscar.setText("Buscar");
        jButton_Buscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_BuscarActionPerformed(evt);
            }
        });

        jButton_Agregar.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        jButton_Agregar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Vista/Imagenes_Files/flechas/adelante/24.png"))); // NOI18N
        jButton_Agregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_AgregarActionPerformed(evt);
            }
        });

        jButton2.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Vista/Imagenes_Files/flechas/atras/24.png"))); // NOI18N

        jLabel_BusquedaProductos.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        jLabel_BusquedaProductos.setText("Búsqueda de Productos");

        jLabel_ProductosSeleccionados.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        jLabel_ProductosSeleccionados.setText("Productos Seleccionados");

        jScrollPane2.setViewportView(tabla_Producto_BusquedaProductos);

        jScrollPane1.setViewportView(tabla_Producto_ProductosSeleccionados);

        jButton_Cancelar.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        jButton_Cancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Vista/Imagenes_Files/cruz/16.png"))); // NOI18N
        jButton_Cancelar.setText("Cancelar");
        jButton_Cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_CancelarActionPerformed(evt);
            }
        });

        jButton_Aceptar.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        jButton_Aceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Vista/Imagenes_Files/tick/16.png"))); // NOI18N
        jButton_Aceptar.setText("Confirmar Selección");
        jButton_Aceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_AceptarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel_BuscarCliente)
                                .addGap(664, 664, 664)
                                .addComponent(label_Buscar481, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jRadioButton_IDProducto)
                                .addGap(18, 18, 18)
                                .addComponent(jRadioButton_Descripcion)
                                .addGap(18, 18, 18)
                                .addComponent(jRadioButton_Precio))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jTextField_Buscar, javax.swing.GroupLayout.PREFERRED_SIZE, 337, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jButton_Buscar)))
                        .addContainerGap(14, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel_BusquedaProductos)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jButton_Agregar, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel_ProductosSeleccionados)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(16, 16, 16))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButton_Aceptar)
                        .addGap(9, 9, 9)
                        .addComponent(jButton_Cancelar)
                        .addGap(23, 23, 23))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(label_Buscar481, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel_BuscarCliente)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jRadioButton_IDProducto)
                            .addComponent(jRadioButton_Descripcion)
                            .addComponent(jRadioButton_Precio))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jTextField_Buscar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jButton_Buscar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_BusquedaProductos)
                    .addComponent(jLabel_ProductosSeleccionados))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(67, 67, 67)
                        .addComponent(jButton_Agregar)
                        .addGap(32, 32, 32)
                        .addComponent(jButton2))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 297, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 297, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 53, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton_Aceptar)
                    .addComponent(jButton_Cancelar))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jRadioButton_DescripcionItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jRadioButton_DescripcionItemStateChanged
        // TODO add your handling code here:
        if (evt.getStateChange() == ItemEvent.SELECTED) {
            this.jRadioButton_Precio.setSelected(false);
        }
    }//GEN-LAST:event_jRadioButton_DescripcionItemStateChanged

    private void jRadioButton_PrecioItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jRadioButton_PrecioItemStateChanged
        // TODO add your handling code here:
        if (evt.getStateChange() == ItemEvent.SELECTED) {
            this.jRadioButton_Descripcion.setSelected(false);
        }
    }//GEN-LAST:event_jRadioButton_PrecioItemStateChanged

    private void jTextField_BuscarFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextField_BuscarFocusGained
        // TODO add your handling code here:
        if (this.jTextField_Buscar.getText().equals("Escriba lo que quiere encontrar...")) {
            this.jTextField_Buscar.setText("");
        }
    }//GEN-LAST:event_jTextField_BuscarFocusGained

    private void jTextField_BuscarKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField_BuscarKeyTyped
        // TODO add your handling code here:
        char c = evt.getKeyChar();
        if (this.jRadioButton_Descripcion.isSelected()) {
            if ((!Character.isWhitespace(c)
                    && !Character.isLetter(c)) || this.jTextField_Buscar.getText().length() > 100) {
                evt.consume();
            }
        }

        
        if (this.jRadioButton_Precio.isSelected()) {
            if (!Character.isDigit(c) || this.jTextField_Buscar.getText().length() > 10) {
                evt.consume();
            }

        }
    }//GEN-LAST:event_jTextField_BuscarKeyTyped

    private void jButton_BuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_BuscarActionPerformed
        // TODO add your handling code here:
        if (!this.jRadioButton_Precio.isSelected()
                && !this.jRadioButton_Descripcion.isSelected() &&
                !this.jRadioButton_IDProducto.isSelected()) {
            JOptionPane.showMessageDialog(V_Principal, "Seleccione algún criterio de búsqueda", "Error", JOptionPane.ERROR_MESSAGE);
        }
        
        if(this.jRadioButton_IDProducto.isSelected()){
            ArrayList<ArrayList> temp = new ArrayList<ArrayList>();
            
            temp.add(Producto.getDatos(this.jTextField_Buscar.getText()));
            this.tabla_Producto_BusquedaProductos.AgregaProductos(temp,true);
            this.tabla_Producto_BusquedaProductos.repaint();
        }
    }//GEN-LAST:event_jButton_BuscarActionPerformed

    private void jButton_CancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_CancelarActionPerformed
        // TODO add your handling code here:
        if(desdeFactura)
        V_Principal.removePanel_BuscaProducto(1);
        
        else if(desdeRecepcion)
        V_Principal.removePanel_BuscaProducto(2);

    }//GEN-LAST:event_jButton_CancelarActionPerformed

    private void jButton_AceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_AceptarActionPerformed
        // TODO add your handling code here:
        //ENVIAR INFORMACION
    }//GEN-LAST:event_jButton_AceptarActionPerformed

    private void jRadioButton_IDProductoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jRadioButton_IDProductoItemStateChanged
        // TODO add your handling code here:
        if (evt.getStateChange() == ItemEvent.SELECTED) {
            this.jRadioButton_Descripcion.setSelected(false);
            this.jRadioButton_Precio.setSelected(false);
        }
    }//GEN-LAST:event_jRadioButton_IDProductoItemStateChanged

    private void jButton_AgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_AgregarActionPerformed
        // TODO add your handling code here:
        ArrayList<ArrayList> temp = this.tabla_Producto_BusquedaProductos.getProductosSeleccionados();
        
        this.tabla_Producto_ProductosSeleccionados.AgregaProductos(temp,false);
        this.tabla_Producto_ProductosSeleccionados.repaint();
    }//GEN-LAST:event_jButton_AgregarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton_Aceptar;
    private javax.swing.JButton jButton_Agregar;
    private javax.swing.JButton jButton_Buscar;
    private javax.swing.JButton jButton_Cancelar;
    private javax.swing.JLabel jLabel_BuscarCliente;
    private javax.swing.JLabel jLabel_BusquedaProductos;
    private javax.swing.JLabel jLabel_ProductosSeleccionados;
    private javax.swing.JRadioButton jRadioButton_Descripcion;
    private javax.swing.JRadioButton jRadioButton_IDProducto;
    private javax.swing.JRadioButton jRadioButton_Precio;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField jTextField_Buscar;
    private Vista.Imagenes_Label.buscar.Label_Buscar48 label_Buscar481;
    private Vista.Tablas.Tabla_Producto tabla_Producto_BusquedaProductos;
    private Vista.Tablas.Tabla_Producto tabla_Producto_ProductosSeleccionados;
    // End of variables declaration//GEN-END:variables
}
